package com.shanzmoo.db.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.db.product.entity.ProductUnitEntity;
import com.shanzmoo.db.product.entity.vo.ProductUnitVO;
import com.shanzmoo.db.product.entity.vo.UnitQueryVO;
import com.shanzmoo.db.product.mapper.ProductUnitMapper;
import com.shanzmoo.db.product.service.IProductUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品单位 服务实现类
 * </p>
 *
 * @author Xueqz
 * @since 2020-12-06
 */
@Service
public class ProductUnitServiceImpl extends ServiceImpl<ProductUnitMapper, ProductUnitEntity> implements IProductUnitService {

    @Override
    public SResponse<PageRespBase<ProductUnitEntity>> getUnitList(UnitQueryVO unitQueryVO) {
        // 开启分页
        Page page = PageHelper.startPage(unitQueryVO.getPageNum(), unitQueryVO.getPageSize());
        //获取信息 -- start
        List<ProductUnitEntity> productUnitEntityList = getUnitListMethod(unitQueryVO);
        //获取信息 -- end
        PageRespBase<ProductUnitEntity> re = PageRespBase.create(page, productUnitEntityList);
        PageHelper.clearPage();
        return SResponse.ok(re);
    }

    @Override
    public List<ProductUnitEntity> getUnitListMethod(UnitQueryVO unitQueryVO) {
        return baseMapper.selectList(new QueryWrapper<ProductUnitEntity>()
                .eq("deleted", false));
    }

    @Override
    public SResponse<List<ProductUnitVO>> getSelectList() {
        List<ProductUnitEntity> entityList = baseMapper.selectList(
                new QueryWrapper<ProductUnitEntity>()
                        .eq("deleted", false)
        );

        List<ProductUnitVO> voList = entityList.stream()
                .map(ProductUnitVO::create)
                .collect(Collectors.toList());

        return SResponse.ok(voList);
    }

}
